<?php
/**
 * Created by IntelliJ IDEA.
 * User: fanxu(746439274@qq.com)
 * Date: 2018/4/21
 * Time: 下午4:31
 */

namespace common\service\transaction;


use GuzzleHttp\Client;

class Eth
{

    const STATUS_NOT_EXIST = -1;
    const STATUS_ING = 0;
    const STATUS_COMPLETE = 1;
    const STATUS_FAILED = 2;
    public static function getTransStatus( $tradeNo )
    {
        $searchUrl = sprintf('https://etherscan.io/tx/%s',$tradeNo);
        $client = new Client([
            'timeout' => 5
        ]);
        $response = $client->request('GET',$searchUrl);
        $html = $response->getBody()->getContents();
        if(strpos($html,'unable')!==false||strpos($html,'Invalid TxHash')!==false){
            return [self::STATUS_NOT_EXIST,''];
        }
        if(strpos($html,'Success')!==false){
            return [self::STATUS_COMPLETE,self::getMoney($html)];
        }
        if(strpos($html,'Fail')!==false){
            return [self::STATUS_FAILED,''];
        }
        return [self::STATUS_ING,''];
    }

    public static function getMoney($html)
    {
        $patten = '/<span title="The amount of ETH to be transferred to the recipient with the transaction">\s+(.*)<\/span>/';
        preg_match($patten,$html,$matches);
        return isset($matches[1])?$matches[1]:'0 Ether';
    }

}